#import "UIColor_SH.h"

@implementation UIColor (SH)

+ (UIColor *)colorWithHexString:(NSString *)str
{
    NSString *target =
        [[[str.lowercaseString stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]
            stringByReplacingOccurrencesOfString:@"x"
                                      withString:@""] stringByReplacingOccurrencesOfString:@"#" withString:@""];
    if (target.length < 1) {
        return nil;
    }
    NSRange range = [target rangeOfString:@"[a-f0-9]*"
                                  options:NSRegularExpressionSearch
                                    range:NSMakeRange(0, target.length)];
    if (range.length > 0 && range.length == target.length) {
        const char *cStr = [target cStringUsingEncoding:NSASCIIStringEncoding];
        long        x    = strtol(cStr, NULL, 16) & 0xFFFFFF;
        return [UIColor colorWithHex:x];
    }
    return nil;
}

+ (UIColor *)colorWithHex:(NSUInteger)col
{
    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r / 255.0f green:(float)g / 255.0f blue:(float)b / 255.0f alpha:1];
}

+ (UIColor *)colorWithHex:(NSUInteger)hexColor alpha:(CGFloat)alpha
{
    float r = ((hexColor >> 16) & 0xFF) / 255.0f;
    float g = ((hexColor >> 8) & 0xFF) / 255.0f;
    float b = (hexColor & 0xFF) / 255.0f;
    return [UIColor colorWithRed:r green:g blue:b alpha:MIN(MAX(alpha, 0.0), 1.0)];
}

+ (UIColor *)colorWithR:(NSUInteger)r g:(NSUInteger)g b:(NSUInteger)b a:(CGFloat)a
{
    return [UIColor colorWithRed:(float)r / 255.0f
                           green:(float)g / 255.0f
                            blue:(float)b / 255.0f
                           alpha:MIN(MAX(a, 0.0), 1.0)];
}

- (UIImage *)toImage
{
    return [self toImageWithSize:CGSizeMake(1, 1)];
}

- (UIImage *)toImageWithSize:(CGSize)size
{
    if (size.width > 0 && size.height > 0) {
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, self.CGColor);
        CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    return nil;
}

+ (instancetype)gradientColorWithSize:(CGSize)size
                            direction:(GradientColorDirection)direction
                           startColor:(UIColor *)startcolor
                             endColor:(UIColor *)endColor {
    
    if (CGSizeEqualToSize(size, CGSizeZero) || !startcolor || !endColor) {
        return nil;
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    
    CGPoint startPoint = CGPointMake(0.0, 0.0);
    if (direction == GradientColorDirectionUpwardDiagonalLine) {
        startPoint = CGPointMake(0.0, 1.0);
    }
    
    CGPoint endPoint = CGPointMake(0.0, 0.0);
    switch (direction) {
        case GradientColorDirectionVertical:
            endPoint = CGPointMake(0.0, 1.0);
            break;
        case GradientColorDirectionDownDiagonalLine:
            endPoint = CGPointMake(1.0, 1.0);
            break;
        case GradientColorDirectionUpwardDiagonalLine:
            endPoint = CGPointMake(1.0, 0.0);
            break;
        default:
            endPoint = CGPointMake(1.0, 0.0);
            break;
    }
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    
    gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:image];
}

@end
